home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Expanders / Scroll / lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  5.0 KB  |  197 lines

  1. /*
  2.  *  LIB.C
  3.  *
  4.  *  Basic Library Resource Handling
  5.  *
  6.  *  NOTE: all data declarations should be initialized since we skip
  7.  *        normal C startup code (unless initial value is don't care)
  8.  *
  9.  *  WARNING: arguments are passed in certain registers from the assembly
  10.  *        tag file, matched to how they are declared below.  Do not change
  11.  *        the argument declarations!
  12.  */
  13.  
  14. #include "DEV_IE:Expanders/defs.h"
  15.  
  16. extern __geta4 struct Library *LibInit   ( __A0 BPTR );
  17. extern __geta4 struct Library *LibOpen   ( __D0 long, __A0 struct Library * );
  18. extern __geta4 long            LibClose  ( __A0 struct Library * );
  19. extern __geta4 long            LibExpunge( __A0 struct Library * );
  20.  
  21. struct Expander *LibBase = NULL;
  22.  
  23. long SysBase        = NULL;
  24. long DOSBase        = NULL;
  25. long IntuitionBase  = NULL;
  26. long GfxBase        = NULL;
  27. long GadToolsBase   = NULL;
  28. BPTR SegList        = 0;
  29. UBYTE *Desc         = NULL;
  30.  
  31. /*
  32.  *    The Initialization routine is given only a seglist pointer.  Since
  33.  *    we are NOT AUTOINIT we must construct and add the library ourselves
  34.  *    and return either NULL or the library pointer.  Exec has Forbid()
  35.  *    for us during the call.
  36.  */
  37.  
  38. struct Library *LibInit( __A0 BPTR segment )
  39. {
  40.  
  41.     struct Library *lib = NULL;
  42.  
  43.     static const long Vectors[] = {
  44.  
  45.     (long)ALibOpen,         /*  standard lib functions    */
  46.     (long)ALibClose,
  47.     (long)ALibExpunge,
  48.     (long)ALibReserved,
  49.  
  50.     (long)IEX_Mount,        /*  mount function            */
  51.  
  52.     (long)IEX_Add,          /*  edit functions            */
  53.     (long)IEX_Remove,
  54.     (long)IEX_Edit,
  55.     (long)IEX_Copy,
  56.     (long)IEX_Make,
  57.     (long)IEX_Free,
  58.     (long)IEX_Refresh,
  59.  
  60.     (long)IEX_Save,         /*  I/O functions             */
  61.     (long)IEX_Load,
  62.  
  63.     (long)IEX_StartSrcGen,  /*  source related functions  */
  64.     (long)IEX_WriteGlobals, 
  65.     (long)IEX_WriteSetup,
  66.     (long)IEX_WriteCloseDown,
  67.     (long)IEX_WriteHeaders,
  68.     (long)IEX_WriteRender,
  69.     (long)IEX_GetIDCMP,
  70.     (long)IEX_WriteData,
  71.     (long)IEX_WriteChipData,
  72.     (long)IEX_WriteOpenWnd,
  73.     (long)IEX_WriteCloseWnd,
  74.     -1
  75.     };
  76.  
  77.     SysBase = *(long *)4;
  78.  
  79.     if( DOSBase = OpenLibrary( "dos.library", 36 )) {
  80.     if( IntuitionBase = OpenLibrary( "intuition.library", 36 )) {
  81.         if( GfxBase = OpenLibrary( "graphics.library", 36 )) {
  82.         if( GadToolsBase = OpenLibrary( "gadtools.library", 36 )) {
  83.  
  84.             if( LibBase = lib = MakeLibrary( (APTR)Vectors, NULL, NULL, sizeof( struct Expander ), NULL )) {
  85.  
  86.             lib->lib_Node.ln_Type = NT_LIBRARY;
  87.             lib->lib_Node.ln_Name = LibName;
  88.             lib->lib_Flags        = LIBF_CHANGED | LIBF_SUMUSED;
  89.             lib->lib_Version      = 37;
  90.             lib->lib_Revision     = 0;
  91.             lib->lib_IdString     = (APTR)LibId;
  92.  
  93.             SegList = segment;
  94.  
  95.             AddLibrary( lib );
  96.             }
  97.  
  98.         }
  99.         }
  100.     }
  101.     }
  102.  
  103.     return( lib );
  104. }
  105.  
  106. /*
  107.  *    Open is given the library pointer and the version request.  Either
  108.  *    return the library pointer or NULL.  Remove the DELAYED-EXPUNGE flag.
  109.  *    Exec has Forbid() for us during the call.
  110.  */
  111.  
  112. struct Library *LibOpen( __D0 long version, __A0 struct Library *lib )
  113. {
  114.     ++lib->lib_OpenCnt;
  115.  
  116.     lib->lib_Flags &= ~LIBF_DELEXP;
  117.  
  118.     return( lib );
  119. }
  120.  
  121. /*
  122.  *    Close is given the library pointer and the version request.  Be sure
  123.  *    not to decrement the open count if already zero.  If the open count
  124.  *    is or becomes zero AND there is a LIBF_DELEXP, we expunge the library
  125.  *    and return the seglist.  Otherwise we return NULL.
  126.  *
  127.  *    Note that this routine never sets LIBF_DELEXP on its own.
  128.  *
  129.  *    Exec has Forbid() for us during the call.
  130.  */
  131.  
  132. long LibClose( __A0 struct Library *lib )
  133. {
  134.     if( lib->lib_OpenCnt && --lib->lib_OpenCnt )
  135.     return( NULL );
  136.  
  137.     if( lib->lib_Flags & LIBF_DELEXP )
  138.     return( LibExpunge( lib ));
  139.  
  140.     return( NULL );
  141. }
  142.  
  143. /*
  144.  *    We expunge the library and return the Seglist ONLY if the open count
  145.  *    is zero.  If the open count is not zero we set the DELAYED-EXPUNGE
  146.  *    flag and return NULL.
  147.  *
  148.  *    Exec has Forbid() for us during the call.  NOTE ALSO that Expunge
  149.  *    might be called from the memory allocator and thus we CANNOT DO A
  150.  *    Wait() or otherwise take a long time to complete (straight from RKM).
  151.  *
  152.  *    Apparently RemLibrary(lib) calls our expunge routine and would
  153.  *    therefore freeze if we called it ourselves.  As far as I can tell
  154.  *    from RKM, LibExpunge(lib) must remove the library itself as shown
  155.  *    below.
  156.  */
  157.  
  158. long LibExpunge( __A0 struct Library *lib )
  159. {
  160.     if( lib->lib_OpenCnt ) {
  161.  
  162.     lib->lib_Flags |= LIBF_DELEXP;
  163.     return( NULL );
  164.     }
  165.  
  166.     Remove( &lib->lib_Node );
  167.  
  168.     FreeMem(( char * )lib - lib->lib_NegSize, lib->lib_NegSize + lib->lib_PosSize );
  169.  
  170.     if( DOSBase ) {
  171.     CloseLibrary( (struct Library *)DOSBase );
  172.     DOSBase = NULL;
  173.     }
  174.  
  175.     if( IntuitionBase ) {
  176.     CloseLibrary( (struct Library *)IntuitionBase );
  177.     IntuitionBase = NULL;
  178.     }
  179.  
  180.     if( GfxBase ) {
  181.     CloseLibrary( (struct Library *)GfxBase );
  182.     GfxBase = NULL;
  183.     }
  184.  
  185.     if( GadToolsBase ) {
  186.     CloseLibrary( (struct Library *)GadToolsBase );
  187.     GadToolsBase = NULL;
  188.     }
  189.  
  190.     if( Desc ) {          /* free the descriptions file  */
  191.     FreeVec( Desc );  /* loaded by IEX_Mount         */
  192.     Desc = NULL;
  193.     }
  194.  
  195.     return(( long )SegList );
  196. }
  197.